package cn.tedu.egnotice1.controller;


import cn.tedu.egnotice1.mapper.NoticeMapper;
import cn.tedu.egnotice1.pojo.dto.NoticeAddParam;
import cn.tedu.egnotice1.pojo.dto.NoticeListQuery;
import cn.tedu.egnotice1.pojo.dto.NoticeUpdateParam;
import cn.tedu.egnotice1.pojo.entity.Notice;
import cn.tedu.egnotice1.pojo.vo.NoticeDetailInfoVO;
import cn.tedu.egnotice1.pojo.vo.NoticeListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;

    @PostMapping("v1/notice/add")
    @ResponseBody
    public String addNotice(NoticeAddParam noticeAddParam){
        System.out.println("插入资讯："+noticeAddParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam,notice);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        notice.setUserId(1L);
        noticeMapper.insertNotice(notice);
        return "插入成功";
    }

    @GetMapping("v1/notice/list")
    @ResponseBody
    public List<NoticeListVO> listNotice(NoticeListQuery noticeListQuery){
        System.out.println("查询资讯："+noticeListQuery);
        List<NoticeListVO> list = noticeMapper.selectNotice(noticeListQuery);
        return list;
    }

    @GetMapping("v1/notice/detail")
    @ResponseBody
    public NoticeDetailInfoVO selectById(Integer id){
        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.selectNoticeDetailInfoVOById(id);
        return noticeDetailInfoVO;
    }

    @PostMapping("v1/notice/del")
    @ResponseBody
    public String delNotice(Integer id){
        noticeMapper.deleteNoticeById(id);
        return "删除成功";
    }

    @PostMapping("v1/notice/update")
    @ResponseBody
    public String updateNotice(NoticeUpdateParam noticeUpdateParam){
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam,notice);
        notice.setUpdateTime(new Date());
        noticeMapper.updateNotice(notice);
        return "更新成功";
    }
}
